/******************************************************************************
 *                  Shanghai ChipON Micro-Electronic Co.,Ltd
 ******************************************************************************
 *  @File Name        : kf8a100fxx_amp.c
 *  @Author           : ChipON AE/FAE Group
 *  @Date             : 2023-12-19
 *  @HW Version       : KF8A100Fxx
 *  @Lib Version      : V2.0.1
 *  @Description      : This file provides the driver for the amp module
 ******************************************************************************
 *  Copyright (C) by Shanghai ChipON Micro-Electronic Co.,Ltd
 *  All rights reserved.
 *
 *  This software is copyright protected and proprietary to
 *  Shanghai ChipON Micro-Electronic Co.,Ltd.
 *****************************************************************************/
/******************************************************************************
 *  |Date        |Version  |Author       |Description
 ******************************************************************************
 *  |2023-12-25  |V2.0     |Xiong Yichao |New creat
 *****************************************************************************/

/******************************************************************************
 **                     QAC Warnings
 *****************************************************************************/
/* PRQA S 0436 EOF #8-bit MCU Features: Direct Write Registers. */
/* PRQA S 1291, 1820, 1821, 1822, 1823, 1851, 1853, 3715, 3757, 3760 EOF #Registers are essentially unsigned numbers. */
/* PRQA S 2754 EOF #Level 7 problem due to unsupported assembly instructions. */
/* PRQA S 3768, 3771, 4130, 4393, 4434, 4443, 4446, 4461, 4532 EOF #Registers are essentially unsigned numbers. */

/******************************************************************************
 *                      Include Files
 *****************************************************************************/
#include "kf8a100fxx_amp.h"
#include "DELAY.h"
/******************************************************************************
 *                      Macro Definitions
 *****************************************************************************/

/******************************************************************************
 *                      Constants
 *****************************************************************************/

/******************************************************************************
 *                      Type definitions
 *****************************************************************************/

/******************************************************************************
 *                      Variables
 *****************************************************************************/

/******************************************************************************
 *                      Function definitions
 *****************************************************************************/

/**
 * @brief AMP Calibrate
 *
 */
void Amp_Calibrate(void)
{
    uint8_t i, dt7_temp = 0u;
    Delay_10us_loop(10u); //  16M 100us delay

    AMPDT    = 0x80u; //  AMPDT default value
    AMPCALEN = 1u;
    Delay_10us_loop(10u); //  16M 100us delay
    dt7_temp = AMPCALD;
    AMPDT7   = dt7_temp;

    AMPDT6 = 1u;
    Delay_10us_loop(10u); //  16M 100us delay
    dt7_temp = AMPDT7;
    if (AMPCALD != dt7_temp)
    {
        AMPDT6 = 0u;
    }
    else
    {
        /* nothing */
    }

    AMPDT5 = 1u;
    Delay_10us_loop(10u); //  16M 100us delay
    dt7_temp = AMPDT7;
    if (AMPCALD != dt7_temp)
    {
        AMPDT5 = 0u;
    }
    else
    {
        /* nothing */
    }

    AMPDT4 = 1u;
    Delay_10us_loop(10u); //  16M 100us delay
    dt7_temp = AMPDT7;
    if (AMPCALD != dt7_temp)
    {
        AMPDT4 = 0u;
    }
    else
    {
        /* nothing */
    }

    AMPDT3 = 1u;
    Delay_10us_loop(10u); //  16M 100us delay
    dt7_temp = AMPDT7;
    if (AMPCALD != dt7_temp)
    {
        AMPDT3 = 0u;
    }
    else
    {
        /* nothing */
    }
    //
    // bit2~bit0 begin form zero
    AMPDT2 = 0u;
    AMPDT1 = 0u;
    AMPDT0 = 1u;
    //
    for (i = 1u; i < 8u; i++)
    {
        Delay_10us_loop(10u); //  16M 100us delay
        dt7_temp = AMPDT7;
        if (AMPCALD != dt7_temp)
        {
            break;
        }
        else
        {
            /* nothing */
        }
        AMPDT += 1u;
    }
    // end Calibrate
    AMPCALEN = 0u;
}

/**
 * @brief AMP enabled
 *
 * @param On disable/enable
 */
void Amp_On(Amp_OnType On)
{
    AMPON = (uint8_t)On;
}

/**
 * @brief AMP Calibrate enabled
 *
 * @param Cal disable/enable
 */
void Amp_CalEn(Amp_CalEnType Cal)
{
    AMPCALEN = (uint8_t)Cal;
}

/**
 * @brief AMP initialization
 * 1. the op-amp outputs should also be set to analog inputs;
 * 2. the op-amps are forced off and cannot be used in hibernation mode.
 *
 * @param InpsSeclect AMP positive seclect
 */
void Amp_Init(Amp_InpsType InpsSeclect)
{
    AMPCTL = 0x01u | (uint8_t)InpsSeclect;
    switch ((uint8_t)InpsSeclect)
    {
    case (uint8_t)AMPINPS_P22:
        TR22  = 1u;
        ANS22 = 1u;
        break;
    case (uint8_t)AMPINPS_P23:
        TR23  = 1u;
        ANS23 = 1u;
        break;
    case (uint8_t)AMPINPS_P15:
        TR15  = 1u;
        ANS15 = 1u;
        break;
    default:
        (void)0u;
        break;
    }

    TR24 = 1u;
    TR25 = 1u;

    ANS24 = 1u;
    ANS25 = 1u;
}

/**
 * @brief AMP Deinitialization
 *
 */
void Amp_DeInit(void)
{
    AMPCTL = 0x80u;
    AMPDT  = 0x80u;
}
/* EOF */
